const query = require('../util/sql')
const fs = require('fs')
const delDir = require('../util/delDir')
module.exports = async (ctx, next) => {
  const newsId = parseInt(ctx.request.query.id)
  // const filename = (newsId + '').padStart(5, '0') + '.json'
  // sql =`select * from news where id=${parseInt(newsId)+1}`;
	// let nextData = await query(sql);
	// sql =`select * from news where id=${parseInt(newsId)-1}`;
	// let prevData = await query(sql);
  // let data = {nextData: nextData[0], prevData: prevData[0]}
  // if (fs.existsSync(`./dist/json/news/${filename}`)) {
  //     ctx.state.data = {...JSON.parse(fs.readFileSync(`./dist/json/news/${(newsId + '').padStart(5, '0')}.json`, 'utf-8')), ...data}
  //     return
  // }
  let sql = `update news set isDel=1 where id=${newsId}`
  let result = await query(sql)
  fs.unlink(`./dist/json/news/${(newsId + '').padStart(5, '0')}.json`, () => console.log(`id=${newsId}的json删除成功!`))
  delDir(`./dist/upload/${(newsId + '').padStart(5, '0')}`)
  ctx.state.data = {
    msg: '删除成功了!'
  }
}